Public Class SummaryDataRecord
    Implements IDataRecord

    Protected docSumField As eUtils.DocSumType
    Private databaseNameField As String = String.Empty

    Public Sub New()

    End Sub

    Public Sub New(ByVal dbName As String, ByVal docSum As eUtils.DocSumType)

        Me.ImportDocSum(dbName, docSum)

    End Sub


    Friend Sub ImportDocSum(ByVal dbName As String, ByVal docSum As eUtils.DocSumType)

        Me.databaseNameField = dbName
        Me.docSumField = docSum

    End Sub

    Public ReadOnly Property ArticleId() As Integer
        Get
            Return Convert.ToInt32(Me.docSumField.Id)
        End Get
    End Property

    Public Property DatabaseName() As String
        Get
            Return Me.databaseNameField
        End Get
        Set(ByVal value As String)
            Me.databaseNameField = value
        End Set
    End Property


    Default Public ReadOnly Property Item(ByVal name As String) As Object Implements System.Data.IDataRecord.Item
        Get
            Return Me.docSumField.Item.GetValue(Me.GetFieldIndex(name))

        End Get
    End Property

    Default Public ReadOnly Property Item(ByVal index As Integer) As Object Implements System.Data.IDataRecord.Item
        Get
            Return Me.docSumField.Item.GetValue(index)

        End Get
    End Property



    Public Overridable Function GetData(ByVal i As Integer) As System.Data.IDataReader Implements System.Data.IDataRecord.GetData

        Throw New NotImplementedException("")

    End Function





#Region "Protected Members"

    '<Obsolete("This method should be avoided if possible. Check the documentation for details.")> _
    ''' <summary>
    ''' Corrects the index of the requested value based on the entrez return type
    ''' </summary>
    ''' <param name="name">Name of the field for the requested value</param>
    ''' <returns>Correct index for the field</returns>
    ''' <remarks>Entrez databases return types with different field lengths depending 
    ''' on the data avialable for the result.
    ''' This method is used to determine the proper index of a given field based on the 
    ''' current result type. This is a workaround and should be avoided if possible.
    ''' If entrez begins returning consistent result types this method should be removed.</remarks>
    Protected Function GetFieldIndex(ByVal name As String) As Integer
        Dim index As Integer = 0

        For Each item As eUtils.ItemType In Me.docSumField.Item

            If String.Equals(item.Name, name, StringComparison.InvariantCultureIgnoreCase) Then

                Return index

            End If

            index += 1

        Next

        Return -1

    End Function

    Protected Function StripPuncuation(ByVal original As String) As String

        Return Text.RegularExpressions.Regex.Replace(original, ":/.;,", String.Empty)

    End Function



    Public Function GetSchemaTable() As System.Data.DataTable

        Dim table As New DataTable("SchemaTable")

        For Each field As eUtils.ItemType In Me.docSumField.Item

            table.Columns.Add(field.Name)

        Next

        Return table

    End Function





    Public ReadOnly Property FieldCount() As Integer Implements System.Data.IDataRecord.FieldCount
        Get
            Return Me.docSumField.Item.Length
        End Get
    End Property

    Public Function GetName(ByVal i As Integer) As String Implements System.Data.IDataRecord.GetName
        Return Me.docSumField.Item(i).Name

    End Function

    Public Function GetOrdinal(ByVal name As String) As Integer Implements System.Data.IDataRecord.GetOrdinal
        Return Me.GetFieldIndex(name)

    End Function



    Protected Function GetDateTime(ByVal docSum As eUtils.DocSumType, ByVal index As Integer) As String

        If docSum.Item(index).Text IsNot Nothing Then

            Return docSum.Item(index).Text(0)

        End If

        Return Date.MinValue.ToShortDateString

    End Function


    Protected Function GetInteger(ByVal index As Integer) As Integer

        If Me.docSumField.Item(index).Text IsNot Nothing AndAlso IsNumeric(Me.docSumField.Item(index).Text(0)) Then

            Return Convert.ToInt32(Me.docSumField.Item(index).Text(0))

        End If

        Return -1

    End Function

    Protected Function GetList(ByVal index As Integer) As String()

        If Me.docSumField.Item(index).Items IsNot Nothing Then

            Dim list As New List(Of String)

            For Each item As eUtils.ItemType In Me.docSumField.Item(index).Items

                For Each text As String In item.Text
                    list.Add(text)
                Next

            Next


            Return list.ToArray

        End If

        Return New String() {}

    End Function


    Protected Function GetStructure(ByVal index As Integer) As Object

        If Me.docSumField.Item(index).Text IsNot Nothing Then

            Return Me.docSumField.Item(index).Text(0)

        End If

        Return New Object

    End Function
#End Region


#Region "DataRecord Methods"


    Public Function GetString(ByVal index As Integer, ByVal stipPuncuation As Boolean) As String

        Return Me.StripPuncuation(Me.getstring(index))

    End Function

    Public Function GetString(ByVal index As Integer) As String Implements System.Data.IDataRecord.GetString

        If Me.docSumField.Item(index).Text IsNot Nothing Then

            Return Me.docSumField.Item(index).Text(0)

        End If

        Return String.Empty

    End Function

    Public Function GetBoolean(ByVal i As Integer) As Boolean Implements System.Data.IDataRecord.GetBoolean

        If Me.docSumField.Item(i).Text IsNot Nothing _
            AndAlso Me.docSumField.Item(i).Text(0) = "1" Then

            Return True

        Else

            Return False

        End If

    End Function

    Public Function GetByte(ByVal i As Integer) As Byte Implements System.Data.IDataRecord.GetByte

        If Me.docSumField.Item(i).Text IsNot Nothing Then

            Return Convert.ToByte(Me.docSumField.Item(i).Text(0))

        End If


    End Function

    Public Function GetBytes(ByVal i As Integer, ByVal fieldOffset As Long, _
        ByVal buffer() As Byte, ByVal bufferoffset As Integer, ByVal length As Integer) As Long _
        Implements System.Data.IDataRecord.GetBytes


        'ToDo: Test This, or mark it as unsupported.
        Dim encoder As New Text.ASCIIEncoding

        Dim bs As Byte() = encoder.GetBytes(Me.GetString(i).ToCharArray(CInt(fieldOffset), length))

        Dim list As New List(Of Byte)(buffer)

        list.RemoveRange(bufferoffset, length)

        list.InsertRange(bufferoffset, bs)

        buffer = list.ToArray

        Return Convert.ToInt64(bs.Length)

    End Function

    Public Function GetChar(ByVal i As Integer) As Char Implements System.Data.IDataRecord.GetChar

        If Me.docSumField.Item(i).Text IsNot Nothing Then

            Return Convert.ToChar(Me.docSumField.Item(i).Text(0))

        End If

    End Function

    Public Function GetChars(ByVal i As Integer, ByVal fieldoffset As Long, ByVal buffer() As Char, ByVal bufferoffset As Integer, ByVal length As Integer) As Long Implements System.Data.IDataRecord.GetChars
        Dim chars As Char() = Me.GetString(i).ToCharArray(Convert.ToInt32(fieldoffset), length)


        'ToDo: Test This, or mark it as unsupported.
        Dim encoder As New Text.ASCIIEncoding

        Dim bs As Char() = Me.GetString(i).ToCharArray(CInt(fieldoffset), length)

        Dim list As New List(Of Char)(buffer)

        list.RemoveRange(bufferoffset, length)

        list.InsertRange(bufferoffset, bs)

        buffer = list.ToArray

        Return Convert.ToInt64(bs.Length)


    End Function



    Public Function GetDataTypeName(ByVal i As Integer) As String Implements System.Data.IDataRecord.GetDataTypeName
        Return Me.docSumField.Item(i).Type.ToString
    End Function

    Public Function GetDateTime(ByVal i As Integer) As Date Implements System.Data.IDataRecord.GetDateTime

        If Me.docSumField.Item(i).Text IsNot Nothing _
            AndAlso IsDate(Me.docSumField.Item(i).Text(0)) Then

            Return Convert.ToDateTime(Me.docSumField.Item(i).Text(0))

        End If

        Return Date.MinValue
    End Function

    Public Function GetDecimal(ByVal i As Integer) As Decimal Implements System.Data.IDataRecord.GetDecimal


        If Me.docSumField.Item(i).Text IsNot Nothing Then

            Return Convert.ToDecimal(Me.docSumField.Item(i).Text(0))

        End If

    End Function

    Public Function GetDouble(ByVal i As Integer) As Double Implements System.Data.IDataRecord.GetDouble

        If Me.docSumField.Item(i).Text IsNot Nothing Then

            Return Convert.ToDouble(Me.docSumField.Item(i).Text(0))

        End If
    End Function

    Public Function GetFieldType(ByVal i As Integer) As System.Type Implements System.Data.IDataRecord.GetFieldType

        Return Me.docSumField.Item(i).GetType

    End Function

    Public Function GetFloat(ByVal i As Integer) As Single Implements System.Data.IDataRecord.GetFloat

        If Me.docSumField.Item(i).Text IsNot Nothing Then

            Return Convert.ToSingle(Me.docSumField.Item(i).Text(0))

        End If
    End Function

    Public Function GetGuid(ByVal i As Integer) As System.Guid Implements System.Data.IDataRecord.GetGuid

        If Me.docSumField.Item(i).Text IsNot Nothing Then

            Return New Guid(Me.docSumField.Item(i).Text(0))
        End If
    End Function

    Public Function GetInt16(ByVal i As Integer) As Short Implements System.Data.IDataRecord.GetInt16

        If Me.docSumField.Item(i).Text IsNot Nothing Then

            Return Convert.ToInt16(Me.docSumField.Item(i).Text(0))

        End If
    End Function

    Public Function GetInt32(ByVal i As Integer) As Integer Implements System.Data.IDataRecord.GetInt32
        If Me.docSumField.Item(i).Text IsNot Nothing Then

            Return Convert.ToInt32(Me.docSumField.Item(i).Text(0))

        End If
    End Function

    Public Function GetInt64(ByVal i As Integer) As Long Implements System.Data.IDataRecord.GetInt64
        If Me.docSumField.Item(i).Text IsNot Nothing Then

            Return Convert.ToInt64(Me.docSumField.Item(i).Text(0))

        End If
    End Function


    Public Function GetValue(ByVal i As Integer) As Object Implements System.Data.IDataRecord.GetValue

        If Me.docSumField.Item(i).Text IsNot Nothing Then

            If Me.docSumField.Item(i).Text.Length = 1 Then

                Return Me.docSumField.Item(i).Text(0)

            Else

                Return Me.docSumField.Item(i).Text

            End If


        ElseIf Me.docSumField.Item(i).Items IsNot Nothing Then

            Return Me.docSumField.Item(i).Items

        Else

            Return New Object

        End If


    End Function

    Public Function GetValues(ByVal values() As Object) As Integer Implements System.Data.IDataRecord.GetValues

        Dim list As New ArrayList

        For i As Integer = 0 To Me.FieldCount - 1

            list.Add(Me.GetValue(i))

        Next

        list.CopyTo(values)


    End Function

    Public Function IsDBNull(ByVal i As Integer) As Boolean Implements System.Data.IDataRecord.IsDBNull
        If Me.docSumField.Item(i).Text IsNot Nothing OrElse _
            String.IsNullOrEmpty(Me.docSumField.Item(i).Text(0)) Then

            Return False

        Else
            Return True

        End If
    End Function


#End Region


End Class
